package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping("/login")
    public Result login() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        String username = user.getUsername();
        System.out.println(username);
        if (user == null) {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
    }

    @RequestMapping("/findMenu")
    public Result findMenu() {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            List<Menu> menuList = userService.findMenulist(username);
            return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findByid")
    public Result findByid(Integer id){
        try {
            com.itheima.pojo.User user = userService.findByid(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    @RequestMapping("/findroleids")
    public Result findroleids(Integer id){
        try {

            Integer[] roleids = userService.findroleids(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user,Integer[] roleids){
        try {
            String password = user.getPassword();
            String s = bCryptPasswordEncoder.encode(password);
            user.setPassword(s);
            userService.add(user,roleids);
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }
    @RequestMapping("/update")
    public Result update(@RequestBody com.itheima.pojo.User user,Integer[] roleids){
        try {
            Result result = this.findByid(user.getId());
            com.itheima.pojo.User user1 = (com.itheima.pojo.User) result.getData();
            if (user.getPassword().equals(user1.getPassword())){
                userService.update(user,roleids);
                return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
            }else {
                String password = user.getPassword();
                String s = bCryptPasswordEncoder.encode(password);
                user.setPassword(s);
                userService.update(user,roleids);
                return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_USER_FAIL);
        }
    }

    @RequestMapping("/deleteByid")
    public Result deleteByid(Integer id){
        try {
            userService.deleteByid(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
    }
}
